<?php

/**
 * Generates a navigation menu.
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version   SVN: $Id: Navigation.php 12 2009-02-28 13:39:31Z monzee $
 */
class Crud_Navigation
{
    /**
     * Generates a UL from array of label => url pairs
     *
     * @param array $links
     * @param int   $active Index of the active elem
     * @return string
     * @todo allow passing of attributes for each LI
     */
    public function navigation($links, $active = null)
    {
        if (empty($links)) {
            return null;
        }

        $count = 0;
        $ret = '<ul>' . PHP_EOL;
        foreach ($links as $key => $val) {
            if (is_int($key)) {
                $link = $desc = $val;
            } else {
                $desc = $key;
                $link = $val;
            }
            
            $li = $active === $count++ ? '<li class="active-tab">' : '<li>';
            $desc = '<span>' . $desc . '</span>';
            $a = null === $link ? $desc : '<a href="' . $link . '">' . $desc . '</a>';

            $ret .= '    ' . $li .  $a  . '</li>' . PHP_EOL;
        }
        $ret .= '</ul>' . PHP_EOL;

        return $ret;
    }

}
